 
jQuery(document).ready(function ($) {   	
	/*loading html advertisement without document.write*/		
	function loadingAdv(item){
		$.ajax({
			url: '/advs/'+item+'.txt',
			success: function (data) {            
				$('#'+item).html(data)
			}
		});
	}
	
	//loadingAdv('advheaderleft');	
	//loadingAdv('advheaderright');
	//loadingAdv('advheaderfull');
	//loadingAdv('adv_left');	

		/*
	var url = document.URL;
	if(url.indexOf("/truyen/")<0){
		//balloon right - test expired at 9h:00 9/1/2014
		var so = new SWFObject("/Images/advs/balloon_right_250-250-2841.swf", "balloonleft-embed", "250", "250", "8", "");
		so.addParam("wmode", "transparent");so.write("balloonleft");
		$('#balloonleft').append('<a title="Close" href="javascript:jQuery(balloonleft).toggle();">X</a>');
	}
	
	//loading header full -- het ngay 24/12
	var so = new SWFObject("http://app.adsoctopus.com/banner/domains/truyengi/1/600truyengi-top.swf", "advheaderfull-embed", "600", "110", "8", "");
	so.addParam("wmode", "transparent");so.write("advheaderfull");	
	*/
	
	/*loading adv---------------------------------------*/
	
    //lazy loading image
    $('[rel="lazyimage"]').each(function () {
        var w = $(this).attr('data-width');
        var h = $(this).attr('data-height');
        var imgsrc = $(this).attr('data-image');
        var title = $(this).attr('title');
        var img = "<img src='" + imgsrc + "' alt='" + title + "' ";
        if (w)
            img += " width=" + w;
		else
			img += " width=auto";
        if (h)
            img += " height=" + h;
		else
			img += " height=auto";
        img += " />";
        $(this).html(img);
    });
    
    //popover quickinfo
    $("[rel=popover]").popover({ html: true, placement: 'above', offset: 10 });

    //menu dropdown
    $('#menu ul').fadeIn(120);
    $('#menu ul li').has('div').addClass('arrow_down');

    loadBookmark(); //loading Box Favourite  

	$("#toTop").click(function (e) {
		$("html, body").animate({
			scrollTop: "0px"
		}, "slow")
	});
	/*
    var cc = 1;
    $('[rel="popover"]').each(function () {
        var a = $(this).find('a');
        var url = a.attr('href');
        if (url) {
            setTimeout(function () {
                $.ajax({ url: url });
            }, cc * 3000);
        }
        cc++;
    });		
	*/
	
    windowResize($(window).width(),$(window).height());
	$(window).resize(function(){
		windowResize($(this).width(),$(this).height());
	});
});

function windowResize(w,h){
	if( w > 1024 && w<1320){
		var sw = (1320-w)/2*(-1);
		$('#adv_left').css({left: sw});
		$('#adv_right').css({right: sw});
	}
	if ( w < 1024 ) {
		$('#adv_left').hide();
		$('#adv_right').hide();
		//$('#balloonleft').hide();
		//$('#balloonright').hide();		
		//$("#toTop").hide();
	} else {
		$('#adv_left').show();
		$('#adv_right').show();
		//$('#balloonleft').show();
		//$('#balloonright').show();			
	}
	
	$(document).scroll(function () {
		if (w < 1100) {
			$("#toTop").hide();
			return
		}
		
		if (($(window).scrollTop()+$(window).height()-200) < h) {
			$("#toTop").hide()
		} else {
			$("#toTop").show()
		}
	})
}
